package com.yc.mvc.web;

import com.yc.mvc.Person;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/5/3
 * Email haijunzhou@hnit.edu.cn
 */
@RestController
public class DemoDo {

    @GetMapping("servletObject")
    Map<String,Object> servletObject(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session,
            InputStream in,
            OutputStream out
    ){
        Map<String,Object> map = new HashMap<>();
        map.put("request",request.toString());
        map.put("response",response.toString());
        map.put("session",session.toString());
        map.put("in",in.toString());
        map.put("out",out.toString());
        return map;
    }

//    Person p0(String name, int age, boolean boy,  ...){
//        return null;
//    }

    // p1?name=zhangsan&age=10&boy=true&like=ppq&like=zq&mather.name=lisi&father.name=wangwu
    @RequestMapping(value= "p1", produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    // 默认表单格式的请求参数
    // 表单请求: a=1&b=2...
    Person p1(Person person){
        return person;
    }

    @RequestMapping(value= "p2", produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    // @RequestBody 用于解析 请求体 是 json 格式的数据
    // json请求: {...}
    Person p2(@RequestBody Person person){
        return person;
    }

    @RequestMapping("add")
    int add(@RequestParam int x, // 映射参数 x
            @RequestParam(name="a") int y,  // 映射参数 a
            @RequestParam(required = true) Integer z,  // Integer 包装器类型参数允许 null 值
            @RequestParam(defaultValue = "100")Integer k
    ){
        return x + y + z + k;
    }

    @RequestMapping("params/{name}")
    String params(@PathVariable String name,
                  @RequestHeader String Host,
                  @RequestHeader("Accept") String accpet,
                  @CookieValue String type){
        String ret = "name = " + name + " ; ";
        ret += "Host = " + Host + " ; ";
        ret += "accpet = " + accpet + " ; ";
        ret += "type = " + type + " ; ";
        return ret;
    }

    @RequestMapping(value = "login",
    produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    public String login(String name, String pwd,
                        HttpSession session){
        if("yc".equals(name) && "123".equals(pwd)){
            session.setAttribute("name",name);
            return "登录成功!<a href='showme'>showme</a>";
        } else {
            return "登录失败!";
        }
    }

    @RequestMapping(path = "showme", produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    public String showme(HttpSession session,
                         @SessionAttribute("name") String myname){
        Object name = session.getAttribute("name");
        System.out.println("name = " + name);
        return "欢迎: " + myname;
    }


}


